#include "stc15f2k60s2.h"
#include "LED.h"
#include "Seg.h"
#include "Key.h"
#include "Tim.h"
#include "stdio.h"

unsigned char pucSeg_Code[8], pucSeg_Buf[9],ucSeg_Pos;
unsigned char ucLed;
unsigned long ulms,ulKey_Time;
unsigned char ucKey_Dly,ucKey_Old;
void Key_Proc(void);
void SEG_Proc(unsigned char ucKey_Val);//拿他键码作为参数实现函数
void main(){
	Cls_Peripheral();
	Timer1Init();
	while(1){
			Key_Proc() ;
			//sprintf(pucSeg_Buf,"%04u",ucSeg);
			Seg_Tran(pucSeg_Buf,pucSeg_Code);
}
}
void Time_1(void) interrupt 3 //定时器1中断函数
{ 
		
		 ulms++; 
		 if(!(ulms % 1000)) 
		 { 
			 ucLed^=1;
			 Led_Disp(ucLed);
		 }
		 if(++ucKey_Dly==10){
			 ucKey_Dly=0;
		 }
		 Seg_Disp(pucSeg_Code,ucSeg_Pos);
		 if(++ucSeg_Pos>=8) ucSeg_Pos=0;
} 
void Key_Proc(void) 
{ 
	 unsigned char ucKey_Val, ucKey_Down, ucKey_Up; 
	 if(ucKey_Dly) return; 
		ucKey_Dly = 1; 
	 ucKey_Val = Key_Read(); 
	 ucKey_Down = ucKey_Val & (ucKey_Old ^ ucKey_Val); 
	 ucKey_Up = ~ucKey_Val & (ucKey_Old ^ ucKey_Val); 
	 ucKey_Old = ucKey_Val; 
	 if(ucKey_Down)  { 
		 ulKey_Time = ulms; 
		 SEG_Proc(ucKey_Down); 
	 } 
	 if(ucKey_Up) 
	 SEG_Proc(ucKey_Up); 
	 if(ucKey_Old && (ulms - ulKey_Time > 1000))  { 
		 ulKey_Time = ulms; 
		 SEG_Proc(ucKey_Old); 
	 } 
}
void SEG_Proc(unsigned char ucKey_Val){
	unsigned int i;
	for(i=0;i<7;i++){
		pucSeg_Buf[i] = pucSeg_Buf[i+1];  
	}
	if(ucKey_Val>=14)
		pucSeg_Buf[i]=ucKey_Val-14+'A';
	else 
		pucSeg_Buf[i]=ucKey_Val-4+'0';
}
